package com.jianggujin.redis.config;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.InputSource;

public class ConfigStore {
    private final static String CONFIG_NAME = "config.xml";

    @SuppressWarnings("unchecked")
    public static List<ConnectionConfig> loadConfig() {
        File configFile = new File(CONFIG_NAME);
        if (configFile.exists()) {
            try (XMLDecoder decoder = new XMLDecoder(new InputSource(new FileInputStream(configFile)))) {
                return (List<ConnectionConfig>) decoder.readObject();
            } catch (Exception e) {
            }
        }
        return new ArrayList<>();
    }

    public static void saveConfig(List<ConnectionConfig> configItems) {
        File configFile = new File(CONFIG_NAME);
        try (XMLEncoder encoder = new XMLEncoder(new FileOutputStream(configFile))) {
            encoder.writeObject(configItems);
            encoder.flush();
        } catch (Exception e) {
        }
    }
}
